#if UNITY_EDITOR
using UnityEngine;
using System.Reflection;
using System.Collections.Generic;
using UnityEditor;
#endif

/// <summary>
/// <para>步骤间跳转标签特殊成员</para>
/// </summary>
public class SwitchTagsJsonSpecialMember : IEditorSpecialMember
{
	#if UNITY_EDITOR

	List<string> GetAllStepTags()
	{
		List<string> allStepTags = TutorialEditorData.Instance.currentScript.GetAllStepTags();
        allStepTags.Insert(0, TutorialManager.endstag);
        allStepTags.Insert(0, TutorialManager.emptyTag);
        return allStepTags;
	}

	public override void SetEditorValue(ref object inst, MemberInfo member, object relativeVal)
	{
		if (TutorialEditorData.Instance.currentScript == null) {
			return;
		}
        


        List<string> allStepTags = GetAllStepTags();
		int emptyIndex = 0;
		var nowValue = (List<string>)relativeVal;
		List<int> retValue = new List<int>(nowValue.Count);
		for (int i = 0; i < nowValue.Count; i++) {
			retValue.Add(emptyIndex);
		}
		for (int i = 0; i < nowValue.Count; i++) {
			for (int j = 0; j < allStepTags.Count; j++) {
				if (allStepTags[j] == nowValue[i]) {
					retValue[i] = j;
					break;
				}
			}
		}
		TutorialJsonUtils.SetValueOfMember(ref inst, member, retValue);
	}

	public override void DisplayInEditor(object inst, float standardWidth, string label, object jeaVal, MemberInfo member, MemberInfo jeaMember, int index = 0)
	{
		if (jeaVal == null || ((List<int>)jeaVal).Count == 0) {
			SetEditorValue(ref inst, jeaMember, TutorialJsonUtils.GetValueOfMember(inst, member));
			jeaVal = TutorialJsonUtils.GetValueOfMember(inst, jeaMember);
		}

        List<string> allStepTags = GetAllStepTags();
		List<int> indexList = (List<int>)jeaVal;
		if (indexList.Count > index) {
			indexList[index] = EditorGUILayout.Popup(label, indexList[index], allStepTags.ToArray(), GUILayout.Width(standardWidth), GUILayout.ExpandWidth(true));
			TutorialJsonUtils.SetValueOfMember(ref inst, jeaMember, indexList);
			List<string> retList = new List<string>(indexList.Count);
			for (int i = 0; i < indexList.Count; i++) {
				int nodeIndex = indexList[i];
				retList.Add(allStepTags[nodeIndex]);
			}
			TutorialJsonUtils.SetValueOfMember(ref inst, member, retList);
		}
	}
	#endif
}
